# @Time : 2024-10-19 9:46
# @Author : Fioman 
# @Phone : 13149920693
"""
元组: 有序且不可变的不同的数据类型的集合.一旦创建,我们就无法更改其值.对应的方法比较少:
tuple(): 创建空元组
count(): 计算元组中指定项的数量
index(): 查找元组中指定项的索引
加号 +   : 连接元组
"""
# region 1. 元组创建
# 空元祖创建
emptyTuple = ()
emptyTuple2 = tuple()
# 含有一个元组的元组
singleTuple = (1,)
singleInt = (1)  # 这个是int类型,如果是元组类型,要加上逗号
print(type(singleTuple), type(singleInt))  # <class 'tuple'> <class 'int'>
tupleVal = (1, 2, 3, "可以有字符串", ["也", "可以", "有列表"], {"还": "可以有字典"})
print(f"tupleVal: {tupleVal}")
# endregion

# region 2. 访问元组元素
exampleTuple = (1, 2, 3, "Fioman", [4, 5, 6])
print(exampleTuple[0])
print(exampleTuple[len(exampleTuple) - 1])
print(exampleTuple[-1])  # 负索引也支持
# endregion

# region 3. 元组的不可变性
# tupleVal = (1, 2, 3, [4, 5, 6])
# tupleVal[0] = 123  # tuple里面的项不可更改,报错
tupleVal = (1, 2, 3, [4, 5, 6])
tupleVal[-1][0] = 444  # 但是如果tuple里面是可变类型,这个可变类型的内容是可以更改的
print(f"tupleVal :{tupleVal}")  # (1, 2, 3, [444, 5, 6])
# endregion

# region 4. 元组的连接(+) 和 重复复制(*)
# 使用+ 号运算来连接两个元组
tup01 = (1, 2, 3)
tup02 = (4, 5, 6)
tupJoint = tup01 + tup02
print(f"tupJoint: {tupJoint}")
# 创建包含100个1,2,3的元组
repeatTuple = (1, 2, 3) * 100
print(f"repeatTuple: {repeatTuple}")
# 检查元素是否在元组中
if 4 in repeatTuple:
    print("在")
else:
    print("不在")
# endregion

# region 5. 元组切片 和 列表一样
tup = ('item1', "item2", 'item3', "item4")
allItems = tup[0:4]
print(allItems)
allItems = tup[0:]
print(allItems)
tup01 = tup[-3:-1]
print(tup01)
tup01 = tup[-1:-3:-1]
print(tup01)

# endregion

# region 6. 将元组更改为列表
tup = (1, 2, 3, 4, 5, 6)
lst = list(tup)
print(lst)
lst[1] = 100
tup = tuple(lst)
print(tup)
# endregion

# region 7. 删除元组,不能删除元组中的项,可以使用del tupleVal 的形式删除整个元组本身
tup01 = (1, 2, 3)
del tup01
tup02 = (4, 5, 6)
del tup02
tup03 = (7, 8, 9)
# del tup03[0]  # 报错 tuple object doesn't support item deletion
# endregion

# region 8. 练习
tup = ()
tupNames01 = ("Fioman", "YueYue", "Hammer")
tupNames02 = ("Mother",)
tupTotal = tupNames01 + tupNames02
print(f"tupTotal: {tupTotal},len(tupTotal) + {len(tupTotal)}")
familyMembers = tupTotal + ("Dad", "Uncle")
print(f"familyMembers: {familyMembers}")

print(f"Daugther: {familyMembers[1]}")
familyList = list(familyMembers)
midOption = familyList[len(familyList) // 2]
print(f"midOption: {midOption}")
firstThree = familyList[:3]
lastThree = familyList[-3:]
print(f"firstThree: {firstThree},lastThree: {lastThree}")
print(f"Fioman is in family: {"Fioman" in familyList})")
print(f"Justy is in family: {"Justy" in familyList}")
# endregion
